package com.easepal802s.project.ui.presenter;


import android.util.Log;

import com.clj.fastble.data.BleDevice;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.ble.HexUtils;
import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.ui.iview.IFraMassageView;
import com.ogawa.base.utils.CommonUtil;

import java.util.Arrays;

/**
 * Created by FHB on 2018/4/1.
 */

public class FraMassagePresenter implements BleDataObserver.OnBleListerner{
    IFraMassageView mView;
    private byte mByteWidth;
    private byte mByte3D;
    private byte mBytePower1;
    private byte mBytePower2;
    private byte mByteSpeed1;
    private byte mByteSpeed2;
    private byte mByteSkill1;
    private byte mByteSkill2;
    private String mFlag="-1";//定点 区间数据
    private int mWidthPosition;
    private int m3DPosition;
    private int mPowerPosition;//力度档位
    private int mSpeedPosition;//速度档位
    private int mAreaPosition;//1定点，2区间
    private int mSkillPosition=-1;//0揉捏，1敲击，2瑞典式，3敲揉，4指压，5推拿；
    private int mSkillStrong;//手法档位；

    public FraMassagePresenter(IFraMassageView view) {
        BleDataObserver.getInst().registerBleDataObserver(this);
        mView = view;
    }
    public void OnDestory(){
        BleDataObserver.getInst().removeBleDataObserver(this);
    }

    @Override
    public void onGetData(String data) {
        if (data.startsWith(BleConstant.BAG1_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            byte byte9 = buffer[9];
            byte byte10 = buffer[10];
            byte byte11 = buffer[11];
            byte bytes5 = buffer[5];
            byte bytes6 = buffer[6];

           if(mByteSkill1!=bytes5||mByteSkill2!=bytes6){
               //按摩手法
               mByteSkill1 = bytes5;
               mByteSkill2 = bytes6;
               if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,5,0))){
                   //推拿
                   mView.getSkill(5,1);
               }
               else {
                   mView.getSkill(5,0);
               }
               if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,5,2))){
                   //揉捏2挡
                   mView.getSkill(0,2);
               }else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,5,1))){
                   //揉捏1挡
                   mView.getSkill(0,1);
               }
               else {
                   mView.getSkill(0,0);
               }
               if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,5,4))){
                   //瑞典2挡
                   mView.getSkill(2,2);
               }else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,5,3))){
                   //瑞典1挡
                   mView.getSkill(2,1);
               } else {
                   mView.getSkill(2,0);
               }

               if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,6,0))){
                   //敲击2挡
                   mView.getSkill(1,2);
               }else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,5,5))){
                   //敲击1挡
                   mView.getSkill(1,1);
               } else {
                   mView.getSkill(1,0);
               }

               if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,6,2))){
                   //指压2挡
                   mView.getSkill(4,2);
               }else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,6,1))){
                   //指压1挡
                   mView.getSkill(4,1);
               } else {
                   mView.getSkill(4,0);
               }
               if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,6,4))){
                   //敲揉2挡
                   mView.getSkill(3,2);
               }else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,6,3))){
                   //敲揉1挡
                   mView.getSkill(3,1);
               } else {
                   mView.getSkill(3,0);
               }

           }


            if(mByteWidth !=byte9){
                //宽度调节
                Log.e("widththth",HexUtils.getByteInPosition(data,9,0)
                        +HexUtils.getByteInPosition(data,9,1)
                        +HexUtils.getByteInPosition(data,9,2));
                mByteWidth = byte9;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,9,2))){
                    mWidthPosition=3;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,9,1))){
                    mWidthPosition=2;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,9,0))){
                    mWidthPosition=1;
                }

                else{
                    mWidthPosition=0;
                }
                mView.getWidth(mWidthPosition);
            }
            if(mBytePower1!=byte9||mBytePower2!=byte10){
                //力度
                mBytePower1 = byte9;
                mBytePower2 = byte10;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,10,1))){
                    mPowerPosition=5;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,10,0))){
                    mPowerPosition=4;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,9,5))){
                    mPowerPosition=3;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,9,4))){
                    mPowerPosition=2;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,9,3))){
                    mPowerPosition=1;
                }

                else {
                    mPowerPosition = 0;
                }
                mView.getPower(mPowerPosition);

            }
            if(mByteSpeed1!=byte10||mByteSpeed2!=byte11){
                //速度
                mByteSpeed1 = byte10;
                mByteSpeed2 = byte11;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,0))){
                    mSpeedPosition=5;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,10,5))){
                    mSpeedPosition=4;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,10,4))){
                    mSpeedPosition=3;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,10,3))){
                    mSpeedPosition=2;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,10,2))){
                    mSpeedPosition=1;
                }

                else {
                    mSpeedPosition = 0;
                }
                mView.getSpeed(mSpeedPosition);


            }

            String dataFlag = HexUtils.getByteInPosition(data,4,0)
                    +HexUtils.getByteInPosition(data,4,1);
            if(!mFlag.equals(dataFlag)){
                //定点区间
                mFlag = dataFlag;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,4,0))){
                    mAreaPosition=1;//定点
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,4,1))){
                    mAreaPosition=2;
                }
                else {
                    mAreaPosition =0;
                }
                mView.getArea(mAreaPosition);
            }


        }
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            //剩余时间
            byte[] buffer = HexUtils.hexToByte(data);
            byte byteMin =buffer[17];
            byte byteSec =buffer[18];
            byte byte12 =buffer[12];
            Log.e("byteData", Arrays.toString(buffer));
//            Log.e("byteMin",byteMin+":"+byteSec);
            mView.getTime(byteMin,byteSec);
            if(mByte3D!=byte12){
                mByte3D = byte12;
                if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,5))){
                    m3DPosition=6;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,4))){
                    m3DPosition=5;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,3))){
                    m3DPosition=4;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,2))){
                    m3DPosition=3;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,1))){
                    m3DPosition=2;
                }
                else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,12,0))){
                    m3DPosition=1;
                }
                else {
                    m3DPosition = 0;
                }
                mView.get3D(m3DPosition);
            }
        }
    }
    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }
    @Override
    public void onPowerOff() {

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air, String name, String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {

    }
}
